package gupao_edu_spring_beans.beans.factory.support;

import gupao_edu_spring_beans.beans.constants.Constants;
import gupao_edu_spring_beans.beans.factory.config.MYBeanDefinition;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;
import java.util.Properties;

/**
 * @ClassName:
 * @Description:
 * @Auther: zhouych
 * @Date: 2019/5/8 18:40
 */
public class MYBeanDefinitionReader {

    private List<String> registyBeanClassNames = new ArrayList<String>();

    private Properties config = new Properties();

    /**
     * 1、查找配置文件
     * 2、扫描配置文件
     * 3、加载匹配到的bean的类名
     * @param configLocations
     */
    public MYBeanDefinitionReader(String[] configLocations) {
        InputStream is = this.getClass().getClassLoader().getResourceAsStream(configLocations[0].replace("classpath:",""));
        try {
            config.load(is);
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            if(null != is){
                try {
                    is.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        doScanner(config.getProperty(Constants.SCAN_PACKAGE));
    }

    /**
     * 1、扫描配置文件中的包路径
     * 2、加载匹配到的bean的类名
     * @param scanPackage
     */
    private void doScanner(String scanPackage) {
        //转换为文件路径，实际上就是把.替换为/就OK了
        URL url = this.getClass().getClassLoader().getResource("/" + scanPackage.replaceAll("\\.","/"));
        File classPath = new File(url.getFile());
        for (File file : classPath.listFiles()) {
            if(file.isDirectory()){
                doScanner(scanPackage + "." + file.getName());
            }else{
                if(!file.getName().endsWith(".class")){ continue;}
                String className = (scanPackage + "." + file.getName().replace(".class",""));
                this.registyBeanClassNames.add(className);
            }
        }
    }

    /**
     * 获取配置文件
     * @return
     */
    public Properties getConfig(){
        return this.config;
    }

    /**
     * 将配置文件中扫描到的信息转换为BeanDefinition
     * @return
     */
    public List<MYBeanDefinition> loadBeanDefinitions(){
        List<MYBeanDefinition> result = new ArrayList<MYBeanDefinition>();
        try {
            for (String className : this.registyBeanClassNames) {
                Class<?> beanClass = Class.forName(className);

                if(beanClass.isInterface()) { continue; }

                result.add(doCreateBeanDefinition(toLowerFirstCase(beanClass.getSimpleName()),beanClass.getName()));

                Class<?> [] interfaces = beanClass.getInterfaces();
                for (Class<?> i : interfaces) {
                    result.add(doCreateBeanDefinition(i.getName(),beanClass.getName()));
                }

            }
        }catch (Exception e){
            e.printStackTrace();
        }
        return result;
    }

    /**
     * 创建beanDefinition
     * @param factoryBeanName
     * @param beanClassName
     * @return
     */
    private MYBeanDefinition doCreateBeanDefinition(String factoryBeanName,String beanClassName) {
        MYBeanDefinition beanDefinition = new MYBeanDefinition();
        beanDefinition.setBeanClassName(beanClassName);
        beanDefinition.setFactoryBeanName(factoryBeanName);
        return beanDefinition;
    }

    /**
     * 首字母转换为小写
     * @param name
     * @return
     */
    private String toLowerFirstCase(String name) {
        char [] chars = name.toCharArray();
        chars[0] += 32;
        return String.valueOf(chars);
    }
}
